package com.info.zhiduoduo.admin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.admin.model.formbean.customer.QueryServiceErrorFormBean;
import com.info.zhiduoduo.admin.model.formbean.customer.SaveServiceErrorFormBean;
import com.info.zhiduoduo.admin.model.formbean.customer.SolveServiceErrorFormBean;
import com.info.zhiduoduo.admin.model.formbean.customer.UpdateServiceErrorFormBean;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.customer.ServiceErrorDto;
import com.info.zhiduoduo.repository.entity.customer.ServiceError;
import com.info.zhiduoduo.repository.mapper.customer.ServiceErrorMapper;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ServiceErrorService extends BaseService {
    @Autowired
    private ServiceErrorMapper serviceErrorMapper;
    @Autowired
    private WxMessageService wxMessageService;

    public SmartResult saveServiceError(BaseSimpleFormBean<SaveServiceErrorFormBean> formBean) {
        if (formBean == null || formBean.getReqdata() == null) {
            return errorParamSmartResult();
        }
        ServiceError serviceError = new ServiceError();
        BeanUtils.copyProperties(formBean.getReqdata(), serviceError);
        serviceError.setCreatePerson(formBean.getUserid());
        if (!serviceError.insert()) {
            return smartResult(StatusMsg.StatusMsg_199);
        }

        //发送微信通知
        SaveServiceErrorFormBean reqdata = formBean.getReqdata();
        boolean messageFlag = wxMessageService.sendNoticeToHuGangForServiceError(reqdata.getContent(), 1, reqdata.getWheatherSolve(), reqdata.getInvolvePerson(), reqdata.getFollowPerson(), null);
        if (!messageFlag) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        return smartResult(serviceError);
    }

    public SmartResult updateServiceError(BaseSimpleFormBean<UpdateServiceErrorFormBean> formBean) {
        if (formBean == null || formBean.getReqdata() == null || formBean.getReqdata().getId() == null) {
            return errorParamSmartResult();
        }
        UpdateServiceErrorFormBean serviceErrorFormBean = formBean.getReqdata();
        ServiceError serviceError = new ServiceError().selectById(serviceErrorFormBean.getId());
        if (null == serviceError) {
            return smartResult(StatusMsg.StatusMsg_102);
        }
        BeanUtils.copyProperties(serviceErrorFormBean, serviceError);
        serviceError.setUpdatePerson(formBean.getUserid());
        serviceError.setUpdatetime(new Date());
        if (!serviceError.updateById()) {
            return smartResult(StatusMsg.StatusMsg_199);
        }

        return smartResult(serviceError);
    }

    public SmartResult solveServiceError(BaseSimpleFormBean<SolveServiceErrorFormBean> formBean) {
        if (null == formBean || null == formBean.getReqdata() || null == formBean.getReqdata().getId() ) {
            return errorParamSmartResult();
        }
        SolveServiceErrorFormBean serviceErrorFormBean = formBean.getReqdata();
        ServiceError serviceError = new ServiceError().selectById(serviceErrorFormBean.getId());
        if (null == serviceError) {
            return smartResult(StatusMsg.StatusMsg_102);
        }
        BeanUtils.copyProperties(serviceErrorFormBean, serviceError);
        serviceError.setSolveTime(new Date());
        if (!serviceError.updateById()) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        ServiceError error = new ServiceError().selectById(serviceErrorFormBean.getId());
        //发送微信通知
        boolean messageFlag = wxMessageService.sendNoticeToHuGangForServiceError(error.getContent(), 2, error.getWheatherSolve(), error.getInvolvePerson(), error.getFollowPerson(),null);
        if (!messageFlag) {
            return smartResult(StatusMsg.StatusMsg_199);
        }
        return smartResult(error);
    }



    public SmartResult<ServiceErrorDto> queryServiceError(BaseSimpleFormBean<QueryServiceErrorFormBean> formBean) {
        if (null == formBean || null == formBean.getReqdata()) {
            return errorParamSmartResult();
        }
        StringBuilder str = new StringBuilder(" 1=1");
        if (formBean.getReqdata().getWheatherSolve() != null) {
            str.append(" and wheather_solve=").append(formBean.getReqdata().getWheatherSolve());
        }
        if (formBean.getReqdata().getContent() != null) {
            str.append(" and content like '%").append(formBean.getReqdata().getContent()).append("%'");
        }
        if (formBean.getReqdata().getCreatePerson() != null) {
            str.append(" and m.managername like '%").append(formBean.getReqdata().getCreatePerson()).append("%'");
        }
        Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        List<ServiceErrorDto> serviceErrorDto = serviceErrorMapper.queryServiceError(str.toString());
        return smartResult(serviceErrorDto, page.getTotal(), page.getPages(), page.getPageNum());

    }

}
